(上)C# 事件event

您所在的位置:网站首页 Disneyland Paris五个组成部分 (上)C# 事件event

(上)C# 事件event

2024-04-25 00:07| 来源: 网络整理| 查看: 265

 初步了解事件

定义:单词Event,翻译为“事件”,能发生的什么事情。

角色:是对象或类 的成员,和属性、方法一样也是对象或类 的成员。事件的作用通知。

方法的作用是处理数据,属性的作用就是存储数据。

事件参数 EventAgrs:就是通知所传递的消息,举个例子:如果手机铃声响力了(通知),手机拥有者可以根据

手机铃声判断,是短线、电话、闹钟,然后采集不同的处理方式。这响铃(名词)就是事件参数,这不同响铃就是 事件参数所包含的信息。(9:10)。

处理事件:微软对事件参数所传递的信息的处理行为称为处理事件,所作事情就叫事情处理器(evert handler)

只管通知,不传参数:例如红绿等。无需额外的消息,事情发生的本身足以说明一切了。

 使用:事件的功能=通知+可选的事件参数。用于对象或者类间的动作协调与信息传递。

C#语言入门详解(020)——事件详解(上): https://www.youtube.com/watch?v=1DhDOJz_S98

 原理:事件模型(event model)中的两个5

1、发生->响应中的5个部分-----------事件模型的组成部分。闹钟| 响了|我| 起床 、孩子 |饿了 |你 |做饭。这里隐藏了"订阅"的关系

2、发生->响应中的5个动作------------(1)我有一个事件(2)一个人 或者一群人 关心我的这个事件(3)我的这个事件发生了(4)关系着个事件的人依次被通知到了(5)被通知到的人根据拿到的事件信息(事件参数)对事件进行事件处理。

 注意:事件的订阅者\事件消息的接收者\事件的响应者\事件的处理者\被事件所通知的对象 都是同一个意思。           事件信息\事件消息发送者(sender)\事件数据\事件参数  都是同一个意思。提示:

1、事件多用于桌面、手机等客户端编程,因为操作系统都是通过事件来驱动的。

2、.MVC、MVP、MVVM等模式,是事件模式更高级、更有效的“玩法” 3、日常开发的时候,使用已有事件的机会比较多,自己声明事件的机会比较少,所以先学使用

4、各种编程语言对这个机制的实现方法不尽相同5、Java语言里没有事件这种成员,也没有委托这种数据类型。Java的“事件"是使用接口来实现的

事件模型的五个组成部分

1、事件拥有者(event source ,对象)

2、事件成员(event 成员)

3、事件响应者(event subsriber,对象)

4、事件处理器(event handler ,方法成员或者委托)---本质上是一个回调方法

5、事件订阅-----------把事件处理器和事件关联在一起,本质上是一种以委托为基础的约定。

(上)C# 事件event -----初识事件以及事件模型的五个组成部分_sed

 

 注意

1、事件处理器是方法成员

2、挂接事件处理器的时候,可以使用委托实例,也可以直接使用方法名字。

3、事件处理器对事件的订阅不是随意。匹配与否由声明事件的委托类型来检测。

4、事件可以同步调用也可以异步调用

 

 第一案例:非常简单,用来熟悉一下事件

(上)C# 事件event -----初识事件以及事件模型的五个组成部分_事件响应_02

 

 

public static void Main(string[] args) { Timer timer = new Timer();//事件拥有者闹钟 Boy boy = new();//事件响应者我 timer.Interval = 1000;//事件触发者 timer.Elapsed += boy.Action;//订阅 先写这个,这时候boy.Action方法还未生成,鼠标放在boy.Action然后ctrl+.在类的内部自动生成方法。原理是事件处理器和事件有共同的签名。我们利用vs的自动功能快速生成方法 timer.Start(); Console.ReadLine();//让界面停在那边 } class Boy { //事件处理器 internal void Action(object sender, ElapsedEventArgs e) { Console.WriteLine("起床");// } }

 第二个案例:

 (上)C# 事件event -----初识事件以及事件模型的五个组成部分_事件模型_03

 

 

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form fom = new Form(); Controler con = new Controler(fom); // Application.Run(fom ); fom.ShowDialog(); } } class Controler { private Form form; public Controler(Form form) { if (form == null) return; this.form = form; this.form.Click += this.Display; } private void Display(object sender, EventArgs e) { this.form.Text = "你点击我啦"; } } }

  

第三个案例:事件的拥有者和事件响应者都是同一个

(上)C# 事件event -----初识事件以及事件模型的五个组成部分_sed_04

 

 

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MyForm my = new MyForm(); my.Click += my.Clicked; Application.Run(my ); } } class MyForm : Form { internal void Clicked(object sender, EventArgs e) { this.Text = "你点击我了"; } } }

第四个案例:最常用的方式 ,事件拥有者是事件响应者的成员。事件的响应者用自己的响应方法订阅了自己的字段成员的某个事件

(上)C# 事件event -----初识事件以及事件模型的五个组成部分_事件处理_05

 

 

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MyForm my = new MyForm(); my.Click += my.Clicked; Application.Run(my ); } } class MyForm : Form { private Button button; private TextBox textBox; public MyForm() { this.button = new Button(); this.textBox = new TextBox(); this.Controls.Add(button); this.Controls.Add(textBox); this.button.Text = "点击我"; textBox.Top = 100; this.button.Click += this.buttonCliked; } private void buttonCliked(object sender, EventArgs e) { textBox.Text = "你点击他了"; } internal void Clicked(object sender, EventArgs e) { this.Text = "你点击我了"; } } }

 (上)C# 事件event -----初识事件以及事件模型的五个组成部分_事件处理_06

 补充案例

一个事件处理器订阅多个事件,然后更具判断不同的事件拥有者处理不同的事情

 

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MyForm my = new MyForm(); Application.Run(my ); } } class MyForm : Form { private Button button; private Button button2; private Button button3; private TextBox textBox; public MyForm() { this.button = new Button { Width = 60, Text = "button1" }; this.button2 = new Button { Top=20,Width=60,Text= "button2" }; this.button3= new Button { Top = 60, Width = 60, Text = "button3" }; this.textBox = new TextBox {Top=100,Width=120 }; this.Controls.Add(button); this.Controls.Add(button2); this.Controls.Add(button3); this.Controls.Add(textBox); this.button.Click += this.buttonCliked; this.button2.Click += this.buttonCliked; this.button3.Click += this.buttonCliked; } private void buttonCliked(object sender, EventArgs e) { if (sender == button) { textBox.Text = "你点击他了button"; }else if (sender == button2) { textBox.Text = "你点击他了button2"; } else if (sender == button3) { textBox.Text = "你点击他了button3"; } ; } } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3